//
//  GLContext.swift
//  learn-gl-es-swift
//
//  Created by LimitLiu on 2019/9/9.
//  Copyright © 2019 LimitLiu. All rights reserved.
//

import UIKit
import GLKit

class GLContext: EAGLContext {
    private var backingClearColor = GLKVector4()
    
    var clearColor: GLKVector4 {
        set {
            doAssert()
            backingClearColor = newValue
            glClearColor(backingClearColor.r, backingClearColor.g, backingClearColor.b, backingClearColor.a)
        }
        get {
            return backingClearColor
        }
    }
    
    func clear(_ mask: GLbitfield) {
        doAssert()
        glClear(mask)
    }
    
    func enable(cap: GLenum) {
        doAssert()
        glEnable(cap)
    }
    
    func disable(cap: GLenum) {
        doAssert()
        glDisable(cap)
    }
}

extension GLContext {
    func doAssert() {
        assert((type(of: self).current() != nil) ? type(of: self).current()!.isKind(of: EAGLContext.self) : false, "Required current context.")
    }
}
